package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.page.PageBeen;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TbItemCatSerive;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("tbItemCat")
public class TbItemCatController {
    @Reference
    private TbItemCatSerive tbItemCatSerive;

    @RequestMapping("itemCat")
    public String itemCat(Model model){
        List<TbTypeTemplate> templateList = tbItemCatSerive.selectTemlate();
        model.addAttribute("templateList",templateList);
        return "admin/item_cat";
    }

    @RequestMapping("getTbItemCat/{parentId}")
    @ResponseBody
    public Result getTbItemCat(@PathVariable("parentId") Long parentId, Model model){
     try {
            List<TbItemCat> tbItemCatList = tbItemCatSerive.getTbItemCat(parentId);
            return Result.response(ResultEnum.SUCCESS).setData(tbItemCatList);
        }catch (Exception e){
            e.printStackTrace();
            List<TbItemCat> pageBeen = tbItemCatSerive.getTbItemCat(parentId);
            return Result.response(ResultEnum.ERROR).setData(e);
        }

    }
    @RequestMapping("addInfo")
    @ResponseBody
    public Result addInfo(TbItemCat tbItemCat){
        int i = tbItemCatSerive.addInfo(tbItemCat);
        return Result.response(ResultEnum.SUCCESS);
    }
    @RequestMapping("toUpdate/{id}")
    @ResponseBody
    public Result toUpdate(@PathVariable("id") Long id){
       TbItemCat tbItemCat = tbItemCatSerive.toUpdate(id);
        return Result.response(ResultEnum.SUCCESS).setData(tbItemCat);
    }
    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbItemCat tbItemCat){
      int i = tbItemCatSerive.updateInfo(tbItemCat);
        return Result.response(ResultEnum.SUCCESS);
    }
    @RequestMapping("deleteInfo/{ids}")
    @ResponseBody
    public Result deleteInfo(@PathVariable("ids")Long[] ids){
      int i = tbItemCatSerive.deleteInfo(ids);
        return Result.response(ResultEnum.SUCCESS);
    }

}
